package reader;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;

public class BufferedReaderDemo1 {

	public static void main(String[] args) {
		InputStream fis = null;
		Reader isr = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		Writer fw = null;
		try {
			// 创建FileInputStream对象
			fis = new FileInputStream("E:/pet.template");
			// 创建InputStreamReader对象
			isr = new InputStreamReader(fis, "gbk");
			// 创建BufferedReader对象
			br = new BufferedReader(isr);
			// 读取文本文件
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			System.out.println("替换前：" + sb);
			String str = sb.toString();
			str = str.replace("{name}", "欧欧");
			str = str.replace("{type}", "狗狗");
			str = str.replace("{master}", "李伟");
			System.out.println("替换后：" + str);
			// 创建FileWriter对象
			// 写入文本文件
			fw = new FileWriter("D:/Program Files/pet.txt");
			// 创建BufferedWriter对象
			bw = new BufferedWriter(fw);
			bw.write(str);
			System.out.println("写入成功，请查看！");
			// System.out.println("替换后"+bw);
			bw.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				// 关闭流
				bw.close();
				fw.close();
				br.close();
				isr.close();
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
